//
//  TWBWelcomeViewController.swift
//  weiBo
//
//  Created by 杨雷 on 2016/12/12.
//  Copyright © 2016年 Yang.L. All rights reserved.
//

import UIKit
import SDWebImage


class TWBWelcomeViewController: UIViewController {
    
    private lazy var bgImageView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "ad_background"))
    
    private lazy var userImageView: UIImageView = {
       
        let imageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
        
        if let imageUrl = TWBUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
            
            imageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
        }
        
        imageView.layer.cornerRadius = 45
        
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        if let name = TWBUserAccountViewModel.sharedUserAccount.userAccount?.name {
            
            label.text = "欢迎回来,\(name)"
            
        } else {
            
            label.text = "欢迎回来"
        }
        label.font = UIFont.systemFont(ofSize: 13)
        
        label.textColor = UIColor.darkGray
        
        label.alpha = 0
        
        return label
    }()
    
    override func loadView() {
        
        view = bgImageView
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setupUI()
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    private func startAnimation() {
        
        userImageView.snp_updateConstraints { (make) in
            
            make.top.equalTo(view).offset(100)
        }
        
        UIView.animate(withDuration: 1, delay: 0.5, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
        }, completion: { (_) -> Void in
            
            UIView.animate(withDuration: 1, animations: { 
                
                self.messageLabel.alpha = 1
            }, completion: { (_) in
                
                NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotificationName), object: nil)
            })
        
        })
        
    }
    
    private func setupUI() {
        
        view.addSubview(userImageView)
        
        view.addSubview(messageLabel)
        
        userImageView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            
            make.top.equalTo(view).offset(300)
            
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        messageLabel.snp_makeConstraints { (make) in
            
            make.top.equalTo(userImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(userImageView)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
